﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChatData;

namespace Client
{
    public partial class Options : Form
    {
        public Options()
        {
            InitializeComponent();
            Setup();
        }
        private void Setup()
        {
            if (Client.Properties.Settings.Default.DefaultFilePath != "")
            {
                this.kryptonFile.Text = Client.Properties.Settings.Default.DefaultFilePath;
            }
            else
            {
                this.kryptonFile.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
            if (Client.Properties.Settings.Default.Tempfiles != "")
            {
                this.kryptonTextBox1.Text = Client.Properties.Settings.Default.Tempfiles;
            }
            else
            {
                this.kryptonFile.Text = System.IO.Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"ClientServerChat");
            }
            if (Client.Properties.Settings.Default.UserName != "" && Client.Properties.Settings.Default.Password != "")
            {
                this.kryptonUsername.Text = Client.Properties.Settings.Default.UserName;
                this.kryptonPassword.Text = Client.Properties.Settings.Default.Password;
                this.kryptonCheckBox1.Checked = true;
            }
            else
            {
                this.kryptonCheckBox1.Checked = false;
            }
            if (Client.Properties.Settings.Default.DefaultPort.ToString() != "")
            {
                this.kryptonIP.Text = Utils.GetLocalIPAddress();
                this.kryptonPort.Text = Client.Properties.Settings.Default.DefaultPort.ToString();
            }
            else
            {
                this.kryptonIP.Text = Utils.GetLocalIPAddress();
                this.kryptonPort.Text = "7000";
            }
            this.kryptonDomain.Text = Client.Properties.Settings.Default.Domain;
        }
        private void Save()
        {
            if (this.kryptonPort.Text == "8080" || this.kryptonPort.Text == "8000")
            {
                MessageBox.Show("Ports 8080 and 8000 are reserved");
                return;
            }
            if (this.kryptonCheckBox1.Checked)
            {
                Client.Properties.Settings.Default.UserName = kryptonUsername.Text;
                Client.Properties.Settings.Default.Password = Utils.EncodePasswordToBase64(kryptonPassword.Text);
                Client.Properties.Settings.Default.Domain = kryptonDomain.Text;
            }
            Client.Properties.Settings.Default.DefaultFilePath = this.kryptonFile.Text;
            Client.Properties.Settings.Default.Tempfiles = this.kryptonTextBox1.Text;
            Client.Properties.Settings.Default.DefaultPort = int.Parse(this.kryptonPort.Text);
            Client.Properties.Settings.Default.Save();
        }
        private void kryptonButtonSave_Click(object sender, EventArgs e)
        {
            Save();
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void kryptonButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
